/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HJT3;

/**
 *
 * @author julio
 */
public class SortAlgorithms {
    
    private int[] list = new int[2000];
    
    public static void main(String Args[]){
        FileGenerator gen = new FileGenerator();
        SortAlgorithms sort = new SortAlgorithms();
        
        //Crea el archivo original y lo mete a una lista
        gen.createOriginalFile();
        sort.setList(gen.getArrayListFile(sort.getList()));
        
        //Lo ordena con el algoritmo insertion
        sort.setList(sort.insertionSort(sort.getList()));
        gen.createSortedFiles("insertion", sort.getList());
        
        //Lo ordena con el algoritmo bubble
        sort.setList(sort.bubbleSort(sort.getList()));
        gen.createSortedFiles("bubble", sort.getList());
        
        //Lo ordena con el algoritmo quick
        sort.setList(sort.quickSort(sort.getList(),sort.getListSpecific(0),sort.getListSpecific(1999)));
        gen.createSortedFiles("quick", sort.getList());
        
        //Lo ordena con el algoritmo merge
        sort.setList(sort.mergeSort(sort.getList(),0,(sort.getList().length-1)));
        gen.createSortedFiles("merge", sort.getList());
        
    }
    
    public int[] insertionSort(int[] arr) {

      int i, j, newValue;
      for (i = 1; i < arr.length; i++) {
            newValue = arr[i];
            j = i;
            while (j > 0 && arr[j - 1] > newValue) {
                  arr[j] = arr[j - 1];
                  j--;
            }
            arr[j] = newValue;
      }
      return arr;
    }
    
    public int[] bubbleSort(int[] arr) {
        boolean swapped = true;
        int j = 0;
        int tmp;
        while (swapped) {
                swapped = false;
                j++;
                for (int i = 0; i < arr.length - j; i++) {         
                    if (arr[i] > arr[i + 1]) {              
                            tmp = arr[i];
                            arr[i] = arr[i + 1];
                            arr[i + 1] = tmp;
                            swapped = true;
                    }
                }      
        }
        return arr;
    }
    
    int partition(int arr[], int left, int right){
        int i = left, j = right;
        int tmp;
        int pivot = arr[(left + right) / 2];
        while (i <= j) {
                while (arr[i] < pivot)
                        i++;
                while (arr[j] > pivot)
                        j--;
                if (i <= j) {
                        tmp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = tmp;
                        i++;
                        j--;
                }
        }
        return i;
    }

 

    public int[] quickSort(int arr[], int left, int right) {
        int index = partition(arr, left, right);
        if (left < index - 1)
                quickSort(arr, left, index - 1);
        if (index < right)
                quickSort(arr, index, right);
        
        return arr;
    }
    
    public int[] mergeSort(int[] array,int lo, int n){
        int low = lo;
        int high = n;
        if (low >= high) {
            return null;
        }

        int middle = (low + high) / 2;
        mergeSort(array, low, middle);
        mergeSort(array, middle + 1, high);
        int end_low = middle;
        int start_high = middle + 1;
        while ((lo <= end_low) && (start_high <= high)) {
            if (array[low] < array[start_high]) {
            low++;
            } else {
                int Temp = array[start_high];
                for (int k = start_high- 1; k >= low; k--) {
                    array[k+1] = array[k];
                }
                array[low] = Temp;
                low++;
                end_low++;
                start_high++;
            }   
        }
        return array;
    }
    
    public int[] getList(){
        return list;
    }
    
    public int getListSpecific(int i){
        return list[i];
    }
    
    public void setList(int[] list){
        this.list = list;
    }
}
